package com.zrrd.jt_admin.image.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.jt_admin.common.bo.ImageClassBo;
import com.zrrd.jt_admin.common.entity.Image;
import com.zrrd.jt_admin.common.entity.ImageClass;
import com.zrrd.jt_admin.common.vo.ImageClassPageVo;
import com.zrrd.jt_admin.image.mapper.ImageClassMapper;
import com.zrrd.jt_admin.image.service.ImageClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.jt_admin.image.service.ImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangYK
 * @since 2024-07-03
 */
@Service
public class ImageClassServiceImpl extends ServiceImpl<ImageClassMapper, ImageClass> implements ImageClassService {

    @Resource
    private ImageService imageService;

    @Override
    public ImageClassPageVo findByPage(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ImageClass> list = this.list();
        PageInfo<ImageClass> pageInfo = new PageInfo<>(list);
        ImageClassPageVo imageClassPageVo = new ImageClassPageVo();

        List<ImageClassBo> imageClassBos = list.stream().map(imageClass -> {
            ImageClassBo imageClassBo = new ImageClassBo();
            BeanUtils.copyProperties(imageClass, imageClassBo);
            Integer count = imageService.lambdaQuery().eq(Image::getImageClassId, imageClass.getId())
                    .count();
            imageClassBo.setImagesCount(Long.valueOf(count));
            return imageClassBo;
        }).collect(Collectors.toList());

        imageClassPageVo.setList(imageClassBos);
        imageClassPageVo.setTotalCount(pageInfo.getTotal());
        return imageClassPageVo;
    }
}
